package com.wiley.homePO;

import io.appium.java_client.android.AndroidDriver;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.wiley.publicMethod.BasePage;
import com.wiley.publicMethod.LoggerControler;

import java.net.MalformedURLException;

public class HotActivityPage extends BasePage {
	
	LoggerControler log;

	/**
	 * 页面元素
	 */
	//热门活动
	public static final By hot_activity_loc = By.name("热门活动");
	
	//列表image
	public static final By hot_activity_image_loc = By.id(BaseId + "image");
	
	//详情页面title
	public static final By title_loc = By.id(BaseId + "title");
	
	//更多选项
	public static final By more_options_loc = By.name("更多选项");
	
	//cover_img
	public static final By cover_img_loc = By.id(BaseId + "cover_img");
		
	//转到上一层级
	public static final By back_loc = By.name("转到上一层级");
	
	/**
	 * 操作方法
	 */
	//点击热门活动
	public void clickHotActivity(){
		this.clickEle(hot_activity_loc);
	}
	
	//循环访问image
	public void clickHotActivityImage() throws InterruptedException{
		int imageSize = this.getElesLength(hot_activity_image_loc);
		for(int i = 0; i < imageSize; i++){
			try{
				this.clickEles(hot_activity_image_loc, i);
				log.info("访问第"+(i+1)+"个活动");
				if(this.isElementPresent(more_options_loc)){
					log.info("转到内嵌页面");
					int cover_img_size = this.getElesLength(cover_img_loc);
					for(int j = 0; j < cover_img_size; j++){
						this.clickEles(cover_img_loc, j);
						log.info("访问第"+(j+1)+"个封面");
						log.info("该封面title为："+this.getTitleName());
						driver.sendKeyEvent(4);
					}
					if(this.isElementPresent(back_loc)){
						driver.sendKeyEvent(4);
						this.clickEle(back_loc);
					}
					log.info("返回热门活动列表页");
				}
				log.info("该活动名为："+this.getTitleName());
				driver.sendKeyEvent(4);
			}catch(Exception e){
				Thread.sleep(1500);
			}
		}
		log.info("当前页面列表访问结束！");
	}

	//获取title名
	public String getTitleName(){
		WebElement ele = this.findEle(title_loc);
		return ele.getText();
	}
	/**
	 * 业务封装
	 * @throws MalformedURLException 
	 * @throws InterruptedException 
	 */
	public void hotActivityView(Class name) throws MalformedURLException, InterruptedException{
		log = this.MyLogger(name);
		AndroidDriver driver = super.SetDriver();
		//滑动到热门活动
		driver.scrollTo("热门活动");
		this.clickHotActivity();
		log.info("点击进入热门活动列表");
		this.clickHotActivityImage();
		log.info("该case测试通过！！！");
	}
}
